package com.video.myapplication;

import android.content.Intent;
import android.media.Image;
import android.media.MediaPlayer;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.video.myapplication.render.ComputerRender;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class VideoActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    private String videoPath = null;
    private SurfaceView surfaceView = null;
    private GLSurfaceView glSurfaceView = null;
    private Button cut = null;
    private TextView levelTextView = null;
    private ImageView imageView = null;
    MediaPlayer player = null;
    private int level = 0;
    private int currentPosition = 0;
    private Timer mTimer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        Intent intent = getIntent();
        String video = intent.getStringExtra("video");
        this.surfaceView = findViewById(R.id.surface);
        this.imageView = findViewById(R.id.image);
        this.surfaceView.getHolder().addCallback(this);

        this.cut = findViewById(R.id.cut);
        this.levelTextView = findViewById(R.id.level);
        this.levelTextView.setText(String.valueOf(level));
        this.videoPath = video;
        Log.e("fuweicong", video);

        this.glSurfaceView = findViewById(R.id.glsv);
        glSurfaceView.setEGLContextClientVersion(3);
        glSurfaceView.setRenderer(new ComputerRender(this, this.imageView));
        glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        glSurfaceView.requestRender();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int count = 0;
                while (true){
                    if (count++ > 10){

                        break;
                    }
                    glSurfaceView.requestRender();
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }


    public void player() throws IOException {
        Log.e("fuweicong", "player");
        player = new MediaPlayer();
        player.setDataSource(this.videoPath);
        player.setSurface(this.surfaceView.getHolder().getSurface());
        player.prepare();
        player.start();
        int duration = player.getDuration();
        int itemLen = duration / 3;
        this.cut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.pause();
                if(level < 2){
                    level += 1;
                    levelTextView.setText(String.valueOf(level));
                    player.seekTo(player.getCurrentPosition() + itemLen);
                } else {
                    level = 0;
                    levelTextView.setText(String.valueOf(level));
                    player.seekTo(player.getCurrentPosition() - itemLen * 2);
                }
                player.start();
            }
        });

        Log.e("fuweicong" , String.valueOf(player.getDuration()));

        mTimer =new Timer();

        TimerTask mTimerTask =new TimerTask() {
            @Override
            public void run() {
                Log.e("fuweicong 进度", String.valueOf(player.getCurrentPosition()) + " " + duration);
                if(player.getCurrentPosition() >  duration / 3 * (level + 1)){
                    currentPosition = player.getCurrentPosition();
                    player.stop();
                    mTimer.cancel();
                }
            }
        };

//每隔10毫秒检测一下播放进度
        mTimer.schedule(mTimerTask, 0, 10);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(player != null)
            player.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mTimer != null)
            mTimer.cancel();
        if(player != null)
            player.stop();
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
//        try {
//            this.player();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }


    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }





    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

    }
}
